/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.mio.core.webapp.controller;

/**
 *
 * @author gjoko.mitrovski
 */
import java.io.IOException;
import java.security.Permission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.appfuse.model.Role;

import org.appfuse.service.GenericManager;
import com.asseco.mio.core.model.CPAuthorization;
import com.asseco.mio.core.model.CpaModel;
import com.asseco.mio.core.model.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;



import org.springframework.web.servlet.mvc.SimpleFormController;

@Controller
@RequestMapping("/cpauthorization*")
public class CPAuthorizationController extends SimpleFormController {


    @ModelAttribute
    public CpaModel cpaModel() {
       ArrayList<Item> itemList = (ArrayList<Item>) itemManager.getAll();

        CpaModel x = new CpaModel();
        x.setCpaList(new ArrayList<CPAuthorization>());
        for (int i=0; i<itemList.size(); i++) {
            CPAuthorization cpa = new CPAuthorization();
            cpa.setItem(itemList.get(i));
            x.getCpaList().add(cpa);
        }
        return x;
    }
    private GenericManager<Item, Long> itemManager;
    private GenericManager<Role, Long> roleManager;
    private GenericManager<CPAuthorization, Long> cpaManager;
    private GenericManager<Permission, Long> permManager;

    @Autowired
    public void setItemManager(@Qualifier("itemManager") GenericManager<Item, Long> itemManager) {
        this.itemManager = itemManager;
    }

    @Autowired
    public void setRoleManager(@Qualifier("roleManager") GenericManager<Role, Long> roleManager) {
        this.roleManager = roleManager;
    }

    @Autowired
    public void setCpaManager(@Qualifier("cpaManager") GenericManager<CPAuthorization, Long> cpaManager) {
        this.cpaManager = cpaManager;
    }
    @Autowired
    public void setPermManager(@Qualifier("permManager") GenericManager<Permission, Long> permManager) {
        this.permManager = permManager;
    }

    
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView handleRequest(HttpServletRequest request)
            throws ServletException, IOException {
        
        //Integer itemSize = itemManager.getAll().size();
        //Integer rolesSize = roleManager.getAll().size();
        //Integer [][] matrix = new Integer [rolesSize][itemSize];
        Map<String, Object> model = new HashMap<String, Object>();
        
        model.put("items", itemManager.getAll());
        model.put("roles", roleManager.getAll());
        List tempCpaList = cpaManager.getAll();
        List permStrings = new ArrayList();
        for (int i=0; i < tempCpaList.size(); i++) {
            CPAuthorization tempCpa = (CPAuthorization) tempCpaList.get(i);
            String s = tempCpa.getCode();
            StringBuilder sb = new StringBuilder(s);
            permStrings.add(sb.reverse());
           
        }
        
        model.put("permStrings", permStrings);
        model.put("cpa", cpaManager.getAll());
        model.put("permissions", permManager.getAll());
        return new ModelAndView("cpauthorization", "model", model);
    }

  
    @RequestMapping(method = RequestMethod.POST)
    protected ModelAndView doSubmit(CpaModel command, HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
     
         CpaModel g  = (CpaModel) command;

         Role temp2 = roleManager.get(g.getCpaList().get(0).getRole().getId());
         List currentCpaList = cpaManager.getAll();
         for(int i = 0; i < g.getCpaList().size(); i++) {
             CPAuthorization tempCpa = g.getCpaList().get(i);
             tempCpa.setRole(temp2);

             for (int j = 0; j < currentCpaList.size(); j++) {
                 CPAuthorization currentCpa = (CPAuthorization) currentCpaList.get(j);
                 if(tempCpa.getItem().getId() == currentCpa.getItem().getId() && tempCpa.getRole().getId() == currentCpa.getRole().getId()) {
                   
                        tempCpa.setId(currentCpa.getId());
                    
                 }
             }
             cpaManager.save(tempCpa);
       }
        // System.out.println(g.getCpaList().get(0).getCode());
        return new ModelAndView("cpauthorization");
        //return "Saving success!";
    }

//    @RequestMapping(method = RequestMethod.POST)
//    @Override
//    void doSubmitAction(Object command) {
//        System.out.println("************SUCCESS");
//    }
    
}
